from configparser import ConfigParser
from pathlib import Path
from typing import Optional


def get_keil_mdk_info(uv4_root: str) -> Optional[dict]:
    """获取 Keil MDK 相关信息"""
    config = ConfigParser()
    root = Path(uv4_root)
    config_file = root / "TOOLS.INI"

    if not config_file.is_file():
        return

    config.read(config_file)

    mdk_info = {}

    rte_path = config.get("UV2", "RTEPATH", fallback="").replace('"', "")
    if rte_path:
        mdk_info["RTEPATH"] = rte_path

    PATH = config.get("ARMADS", "PATH", fallback="").replace('"', "")
    PATH1 = config.get("ARMADS", "PATH1", fallback="").replace('"', "")

    armclang_path = Path(PATH) / Path(PATH1)

    mdk_info["ARMCLANG_PATH"] = str(armclang_path)

    return mdk_info


if __name__ == "__main__":
    uv4_root = str(Path.home() / "AppData" / "Local" / "Keil_v5")
    print(f"Using UV4 root: {uv4_root}")
    mdk_info = get_keil_mdk_info(uv4_root)
    print(mdk_info)
